<template>
  <div class="container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card shadow="never">
        <!-- 面包屑跳转模式 -->
        <el-breadcrumb v-if="subjectID" class="bread el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ this.$route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>

        <el-menu>
          <el-menu-item>
            <el-form class="form">
              <el-form-item class="ins" label="目录名称">
                <el-input class="ipt" v-model="form.tagName"></el-input>
              </el-form-item>
              <el-form-item
                style="padding: 0 50px 0 80px"
                class="ins"
                label="状态"
              >
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option
                    class="option"
                    v-for="obj in base.status"
                    :key="obj.id"
                    :value="obj.id"
                    :label="obj.value"
                    >{{ obj.value }}</el-option
                  >
                </el-select>
              </el-form-item>
              <el-form-item class="ins">
                <el-button @click="clearCon">清除</el-button>
                <el-button type="primary" @click.native="searchTages"
                  >搜索</el-button
                >
              </el-form-item>
              <el-form-item class="ins ins3" v-if="subjectID">
                <i
                  style="color: #1fa0fb; font-size: 12px"
                  class="el-icon-back"
                  @click="$router.back()"
                  >返回学科</i
                >
              </el-form-item>
              <el-form-item class="ins ins2">
                <el-button
                  style="font-size: 12px"
                  type="success"
                  @click="showDialog = true"
                  >新增目录</el-button
                >
              </el-form-item>
            </el-form>
          </el-menu-item>
        </el-menu>
        <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon
          :closable="false"
        ></el-alert>
        <!-- 表格数据部分 -->
        <el-table
          :data="tagList"
          :header-cell-style="{ background: '#fafafa', color: '#6060266' }"
          style="width: 100%; font-size: 12px"
        >
          <el-table-column
            prop="date"
            label="序号"
            type="index"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="subjectName"
            label="所属学科"
            width="180"
          ></el-table-column>
          <el-table-column prop="tagName" label="标签名称"></el-table-column>
          <el-table-column prop="username" label="创建者"></el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template slot-scope="scope">
              {{ changeTime(scope.row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              {{ scope.row.state ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="forbid(scope.row)">{{
                scope.row.state ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                size="small"
                :disabled="Boolean(scope.row.state)"
                @click="editTags(scope.row)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                :disabled="Boolean(scope.row.state)"
                @click="delTags(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" class="top" justify="end" style="height: 50px">
          <el-pagination
            background
            layout="total,prev,pager,next,sizes,jumper"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pagesize"
            @size-change="sizeChange"
            @current-change="currentChange"
            :page="page"
            :total="total"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <TaggsAdd
      ref="edit"
      @update="update"
      @close="close"
      :subjectID="subjectID"
      :showDialog="showDialog"
    ></TaggsAdd>
  </div>
</template>

<script>
// 数据接口
import { list, changeState, remove } from '@/api/hmmm/tags'
// 时间格式处理
import dayjs from 'dayjs'
// 弹层
import TaggsAdd from '../components/tags-add.vue'
// 状态栏
import base from '../../api/base/baseApi'
export default {
  components: {
    TaggsAdd
  },
  data () {
    return {
      tagList: [],
      page: 1,
      pagesize: 10,
      subjectID: this.$route.query.id,
      form: {
        tagName: '',
        state: null,
        subjectID: ''
      },
      base,
      total: 0, // 数据条数
      showDialog: false, // 弹层的状态
      id: ''
    }
  },
  created () {
    this.getTagList()
  },
  methods: {
    // 数据铺设
    async getTagList () {
      const res = await list({
        page: this.page,
        pagesize: this.pagesize,
        subjectID: this.subjectID
      })
      console.log(res)
      this.total = res.data.counts
      this.tagList = res.data.items
    },
    // 时间处理

    // 清框功能
    clearCon () {
      this.form = {
        tagName: '',
        state: ''
      }
      this.getTagList()
    },
    // 搜索功能
    async searchTages () {
      if (this.form.state === '' && this.form.tagName === '') {
        return this.getTagList()
      }
      if (this.subjectID) {
        this.form.subjectID = this.subjectID
        const res = await list(this.form)
        this.tagList = res.data.items
        this.total = res.data.counts
      } else if (!this.form.state) {
        const res = await list({
          tagName: this.form.tagName
        })
        this.tagList = res.data.items
        this.total = res.data.counts
      } else {
        const res = await list({
          tagName: this.form.tagName,
          state: this.form.state
        })
        this.tagList = res.data.items
        this.total = res.data.counts
      }
    },
    // 时间处理
    changeTime (data) {
      return dayjs(data).format('YYYY-MM-DD HH:mm:ss')
    },
    // 修改状态 状态1：启用：0：禁用
    changeState (data) {
      const maps = {}
      base.status.forEach(item => {
        maps[item.id] = item.value
      })
      return maps[data]
    },
    // 修改禁用功能
    async forbid (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState(row)
      this.$message.success('修改成功')
      this.getTagList()
    },
    update () {
      this.getTagList()
    },
    close () {
      this.showDialog = false
    },
    editTags (row) {
      this.showDialog = true
      this.$refs.edit.getEditTag(row)
    },
    // 删除
    delTags (row) {
      // 删除操作
      this.$confirm('确定删除吗？', '提示').then(async () => {
        await remove(row)
        this.$message.success('删除成功')
        if (this.page > Math.ceil((this.counts - 1) / this.pagesize)) {
          this.page = Math.ceil((this.counts - 1) / this.pagesize)
        }
        this.getTagList()
      }).catch(() => {
        console.log('取消了删除')
      })
    },
    // page改变触发
    async sizeChange (data) {
      this.pagesize = data
      this.getTagList()
    },
    // pagesize改变触发
    currentChange (data) {
      this.page = data
      this.getTagList()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-breadcrumb {
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
  font-size: 12px;
}

::v-deep .el-card__body {
  padding: 10px;
}
.dialog {
  float: right;
  padding: 20px 30px;
}
::v-deep .dialog .el-input--medium .el-input__inner {
  width: 50px;
}
::v-deep .el-input--medium .el-input__inner {
  /*width: 200px;*/
  height: 30px;
  padding: 0;
  margin-right: 80px;
}
.ins {
  display: inline-block;
  height: 30px;
  line-height: 30px;
}
.ins1 {
  padding: 0 10px;
}
.form {
  display: flex;
  align-items: center;
}
::v-deep .el-form-item--medium .el-form-item__content,
.el-form-item--medium .el-form-item__label {
  height: 30px;
  line-height: 30px;
}
::v-deep.el-form-item__label {
  font-size: 12px;
}
.el-button--medium {
  padding: 7px 20px;
}
.ins2 {
  position: absolute;
  right: 0;
}
.ins3 {
  position: absolute;
  right: 130px;
}
::v-deep.el-button--small,
.el-button--small.is-round {
  padding: 7px 0px;
}
::v-deep.el-form-item__content {
  height: 25px;
  line-height: 25px;
}
.el-icon-edit:before {
  color: #ffffff;
}
.total {
  height: 30px;
  line-height: 30px;
  background-color: #f4f4f5;
  padding-left: 15px;
  font-size: 13px;
  border-radius: 10px;
}
.total span {
  margin-left: 15px;
  display: inline-block;
  font-size: 13px;
}
::v-deep .el-menu {
  padding-top: 10px;
}
::v-deep .el-menu-item {
  padding: 0;
  height: 38px;
  line-height: 38px;
}
::v-deep .el-input--medium {
  width: auto;
}
::v-deep .el-input--medium .el-input__inner {
  font-size: 12px;
  padding-left: 10px;
}
button {
  font-size: 12px;
}
</style>
